﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace RhythmGame
{
    internal class ScoresRecord
    {
        public struct BeatmapRecord
        {
            public string Name;
            public string BeatMapName;
            public int Score;
            public string Accuracy;
            public int CritalPerfectCount;
            public int PerfectCount;
            public int GreatCount;
            public int GoodCount;
            public int MissCount;
            public DateTime Time;
            // 其他字段可以根据需要添加
        }

        public List<BeatmapRecord> Records { get; set; } = new List<BeatmapRecord>();

        public static ScoresRecord Instance { get; set; } = new ScoresRecord();

        private ScoresRecord() { }

        public static void AddRecord(BeatmapRecord record)
        {
            Instance.Records.Add(record);

            // 保存记录到文件
            string filePath = Path.Combine("Scores", $"player_{Settings.Instance.PlayerName}_records.json");
            // 文件不存在就创建一个新的空文件
            if (!File.Exists(filePath))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                File.WriteAllText(filePath, "[]"); // 初始化为空数组
            }

            // 读取现有记录
            var existingRecords = File.Exists(filePath)
                ? JsonConvert.DeserializeObject<List<BeatmapRecord>>(File.ReadAllText(filePath)) ?? new List<BeatmapRecord>()
                : new List<BeatmapRecord>();

            // 添加新记录
            existingRecords.Add(record);

            // 保存回文件
            File.WriteAllText(filePath, JsonConvert.SerializeObject(existingRecords, Formatting.Indented));
        }
    }
}
